package com.zj.web.controller;

import com.zj.web.bean.ResponseResult;
import lombok.extern.java.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authorization.AuthorizationDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * spring mvc 的controller类  全局异常处理类
 */
@ControllerAdvice
@Log
public class GlobalExceptionHandler {


    @ExceptionHandler(AuthorizationDeniedException.class)
    public  ResponseEntity<ResponseResult> handleRuntimeExcepiton(AuthorizationDeniedException ex) {
        ex.printStackTrace();
        log.severe( "用户访问未授权异常处理:"+ ex.getMessage() );
        // 自定义异常处理逻辑
        return new ResponseEntity<>( ResponseResult.error("用户访问未授权异常:" + ex.getMessage()), HttpStatus.UNAUTHORIZED);
    }

    //TODO: 处理自定义异常
    @ExceptionHandler(RuntimeException.class)
    public  ResponseEntity<ResponseResult> handleRuntimeExcepiton(RuntimeException ex) {
        ex.printStackTrace();
        log.severe( "业务异常处理:"+ ex.getMessage() );
        // 自定义异常处理逻辑
        return new ResponseEntity<>( ResponseResult.error("用户服务模块致命错误:" + ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }


    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseResult> handleAllException(Exception ex) {
        ex.printStackTrace();
        log.severe( "全局异常处理:"+ ex.getMessage() );
        // 自定义异常处理逻辑
        return new ResponseEntity<>( ResponseResult.error("用户服务模块致命错误:" + ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
